package org.lagou.edu.stock.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface StockMapper {

    /**
     * 库存扣减
     * @param shopId 扣减商品id
     * @param num 扣减个数
     * @return 扣减失败返回0; 扣减成功返回非0；
     */
    @Update("update stock set quantity = quantity - #{num} where shop_id = #{shopId} and quantity >= #{num}")
    int deduce(long shopId, long num);

    /**
     * 获取对于shopId的库存数量
     * @param shopId 商品id
     * @return
     */
    @Select("select quantity from stock where shop_id = #{shopId}")
    long getQuantity(long shopId);
}
